//
//  SettingsView.swift
//  maozzi-rss
//
//  Created by 帽子 on 2020/6/13.
//  Copyright © 2020 帽子. All rights reserved.
//	系统设置 view

import SwiftUI

struct SettingsView: View {
    @Binding var isPresented: Bool
    @State var isPop: Bool = false
    @State var isInterfaceStyle: Bool = false
    @EnvironmentObject var userData: UserData

    var body: some View {
        NavigationView {
            List {
                Section(header: Text("界面设置").font(.caption)) {
                    Button("首页设置", action: {
                    })
                    Button(action: {
                        self.isPop.toggle()
                    }) {
                        HStack {
                            Text("未读列表设置")
                            Spacer()
                            if self.userData.unreadListStyle == 3 {
                                Text("三栏").font(.subheadline).foregroundColor(.secondary)
                            } else {
                                Text("两栏").font(.subheadline).foregroundColor(.secondary)
                            }
                            Image(systemName: "chevron.right")
                                .padding(.horizontal)
                        }
                    }.actionSheet(isPresented: self.$isPop, content: {
                        ActionSheet(title: Text("未读列表设置"), message: Text(""), buttons: [
                            .default(Text("两栏"), action: {
                                self.userData.unreadListStyle = 2
                                UserDefaults.standard.set(2, forKey: "unreadListStyle")
                            }),
                            .default(Text("三栏"), action: {
                                self.userData.unreadListStyle = 3
                                UserDefaults.standard.set(3, forKey: "unreadListStyle")
                            }),
                            .cancel(Text("取消")),
                        ])
                    })
                    Button(action: {
                        self.isInterfaceStyle.toggle()
                    }) {
                        HStack {
                            Text("黑暗模式设置")
                            Spacer()
                            Image(systemName: "chevron.right")
                                .padding(.horizontal)
                        }
                    }.actionSheet(isPresented: self.$isInterfaceStyle, content: {
                        ActionSheet(title: Text("黑暗模式设置"), message: Text(""), buttons: [
                            .default(Text("跟随系统"), action: {
                                self.userData.interfaceStyle = 2
                                UserDefaults.standard.set(2, forKey: "interfaceStyle")
                            }),
                            .default(Text("开启"), action: {
                                self.userData.interfaceStyle = 1
                                UserDefaults.standard.set(1, forKey: "interfaceStyle")
                            }),
                            .default(Text("关闭"), action: {
                                self.userData.interfaceStyle = 0
                                UserDefaults.standard.set(0, forKey: "interfaceStyle")
                                
                            }),
                            .cancel(Text("取消")),
                        ])
                    })
                }.padding(.leading)
                Section(header: Text("数据源设置").font(.caption)) {
                    // 利用组件层次性（ZStack），隐藏原有的NavigationLink 小箭头样式
                    ZStack {
                        HStack {
                            Text("多数据源")
                            Spacer()
                            Image(systemName: "chevron.right")
                                .padding(.horizontal)
                        }
                        NavigationLink(destination: DatasourceView().environmentObject(self.userData)) {
                            EmptyView()
                        }.buttonStyle(PlainButtonStyle())
                    }
                }.padding(.leading)
            }.navigationBarTitle("用户设置", displayMode: .inline)
                .navigationBarItems(trailing: Button("完成", action: {
                    self.isPresented.toggle()
                }))
        }
    }
}
